package cn.dsp.admin.service.impl;

import cn.dsp.admin.entity.RolePerm;
import cn.dsp.admin.entity.admin.Role;
import cn.dsp.admin.mapper.RoleMapper;
import cn.dsp.admin.service.IRoleService;
import cn.dsp.admin.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void addRole(Role role,String createdUser) {
        String roleName = role.getRoleName();
        if (roleName.equals("")){
            throw new InsertException("角色名称不能为空！！");
        }
        Role result = findByRoleName(roleName);
        if (result != null) {
            throw new UsernameConflictException("角色名:"+roleName+",已存在！");
        }
        Date now = new Date();
        role.setCreatedUser(createdUser);
        role.setCreatedTime(now);
        role.setModifiedUser(createdUser);
        role.setModifiedTime(now);
        save(role);
    }

    @Override
    public void changeInfo(Integer roleId, String modifiedUser, Role role) {
        Role result = findByRoleId(roleId);
        if (result==null){
            throw new RoleNotFoundException("角色有误");
        }
        role.setRoleId(roleId);
        role.setModifiedUser(modifiedUser);
        role.setModifiedTime(new Date());
        updateInfo(role);
    }

    @Override
    public Role getByRoleId(Integer roleId) {
         Role result =findByRoleId(roleId);
        if (result == null) {
            throw new RoleNotFoundException("角色信息不存在！");
        }
         return result;
    }


    @Override
    public Role getByRoleName(String roleName) {
        Role result = findByRoleName(roleName);
        if (result == null) {
            throw new UserNotFoundException("未找到该角色！");
        }
        return result;
    }

    @Override
    public void deleteByRoleId(Integer roleId) {
        findByRoleId(roleId);
       remove(roleId);
    }

    @Override
    public List<Role> getAllRole() {
        return findAllRole();
    }

    @Override
    public List<Role> getLikeRoleName(String roleName) {
        if (roleName.equals("FIND_RILE_NAME_NULL")){
            return findAllRole();
        }
        return findLikeRoleName(roleName);
    }
    /**给角色添加权限*/
    @Override
    public void roleAddPerm(RolePerm rolePerm) {
        Integer rows = roleMapper.roleAddPerm(rolePerm);
        if (rows!=1){
            throw new InsertException("添加失败！");
        }
    }
    /**移出角色的权限*/
    @Override
    public void deleteAllPermByRid(Integer roleId) {
        Integer rows = roleMapper.removeAllPerm(roleId);
        if (rows<0){
            throw new DeleteException("删除失败！");
        }
    }

    /**************************************************************************************************************************/
    /**
     * 添加新角色
     * @param role 角色类实例
     * @return 返回受影响行数
     */
    private void save(Role role){
        Integer rows = roleMapper.save(role);
        if (rows != 1) {
            throw new InsertException("插入数据时出现位置错误");
        }

    }




    /**
     * 修改角色信息
     * @param role
     * @return
     */
    private void updateInfo(Role role){
        Integer rows = roleMapper.updateInfo(role);
        if (rows!=1){
            throw new UpdateException("更新数据时发生位置错误！");
        }
    }

    /**
     * 根据查询角色信息
     * @param roleId 角色id
     * @return 返回查询到的角色信息
     */
    private Role findByRoleId(Integer roleId){
       return roleMapper.findByRoleId(roleId);
    }


    /**
     * 根据角色名称查询角色信息
     * @param roleName 角色名称
     * @return 返回角色信息
     */
    private Role findByRoleName(String roleName){
        return roleMapper.findByRoleName(roleName);
    }

    /**
     * 显示所有角色信息
     * @return
     */
    private List<Role> findAllRole(){
        return roleMapper.findAllRole();
    }

    /**
     *  根据角色名称查询符合的角色信息
     * @param roleName 相关字段
     * @return 查询到的结果
     */
    private List<Role> findLikeRoleName(String roleName){
        return roleMapper.findLikeRoleName(roleName);
    }

    /**
     * 删除角色信息数据
     * @param roleId 角色id
     * @return 受影响行数
     */
    private void remove(Integer roleId){
        Integer rows = roleMapper.remove(roleId);
        if (rows!=1){
            throw new DeleteException("删除数据错误！");
        }
    }
}
